#pragma once

#include "const.h"
#include "message.grpc.pb.h"

#include <grpc++/grpc++.h>
#include <vector>

struct ChatServer
{
	ChatServer(): host(""), port(""), name(""), con_count(0) {

	}

	ChatServer(const ChatServer& cs) : host(cs.host), port(cs.port), name(cs.name), con_count(cs.con_count) {
	
	}

	ChatServer& operator=(const ChatServer& cs) {
		if (&cs == this) {
			return *this;
		}

		host = cs.host;
		name = cs.name;
		port = cs.port;
		con_count = cs.con_count;

		return *this;
	}

	std::string host;
	std::string port;
	std::string name;
	int con_count;
};

// grpc服务端
class StatusServiceImplement final : public message::StatusService::Service
{
public:
	StatusServiceImplement();
	// 获取ChatServer的Ip和Token
	grpc::Status GetChatServer(grpc::ServerContext* context, const message::GetChatServerReq* request, 
		message::GetChatServerRsp* reply) override;
	grpc::Status Login(grpc::ServerContext* context, const message::LoginReq* request,
		message::LoginRsp* reply) override;

private:
	// 向redis中插入token
	void insertToken(int uid, std::string token);
	std::string generate_unique_string();
	// TODO 可采用Sorted Set实现，以负载均衡的方式分配服务器
	ChatServer getChatServer();

public:
	std::mutex m_server_mtx;
	std::map<std::string, ChatServer> m_servers;
 };

